import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class PeerSend extends Thread{
	
	PeerData peerData; // This will have peer variables like peerID, peer server name and other buffers we might have to use
	Message recievedMsg;
	Socket socket = null;
	private ObjectInputStream objIn;
	private ObjectOutputStream objOut;
	public void initPeerThread(){
		String THREADTYPE = "SEND"; 
		
		//hard coded variables
		String peerName = "127.0.0.1";
		int peerPortNumber = 42110;
		Socket socket = null;
		//Start Up Code
		try {
			socket = new Socket(peerName,peerPortNumber);
			objIn = new ObjectInputStream(socket.getInputStream());
			objOut = new ObjectOutputStream(socket.getOutputStream());
			Logger.log("TCP connection");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} //Connecting to peer with peerID < peerProp.peerID
		
		
		HandshakeMessage hMsg = new HandshakeMessage(objIn,objOut);
		hMsg.sendMessage();
		if(hMsg.recvMessage()){ //
			BitfieldMessage bMsg = new BitfieldMessage(objIn,objOut);
			bMsg.sendMessage();
			recievedMsg =  bMsg.recvMessage();
			boolean isInterested = peerData.checkBitfield(recievedMsg.getPayload());
			if(isInterested){
				InterestedMessage iMsg = new InterestedMessage(objIn,objOut);
				iMsg.sendMessage();
			}
		}
		else{
			//To Do - print error
		}
	}
}
